import { observable, action } from 'mobx';
import { Form } from 'weaver-mobile-page';
import { URGENT_CONDITION } from '../../util/Urgent';
import * as Api from '../../api';

const defaultState = {
  path: '',
  title: '',
  content: null,
  emailForm: null,
  messageForm: null, 
  smsForm: null,
  defaultcontent: null
}
export default class UrgentStore {
    path = defaultState.path;
    title = defaultState.title;
    @observable content = defaultState.content;
    @observable emailForm = defaultState.emailForm;
    @observable messageForm = defaultState.messageForm;
    @observable smsForm = defaultState.smsForm;
    @observable defaultcontent = defaultState.defaultcontent;

    @action setStoreState = (params) => {
        Object.keys(params).forEach(key => {
          this[key] = params[key];
        })
    }

    @action clear = () => this.setStoreState(defaultState)

    @action initialState = async (key, path, title, params) => {
        let condition = URGENT_CONDITION(key, title);
        this[`${key}Form`] = new Form.Store({ route: path, id: key });
        this[`${key}Form`].initForm(condition.conditioninfo, path);
          
        // 初始化参数
        if (!this.content) {
            let res = await Api.getUrgentContent(params)
            if (res.status) {
              this.defaultcontent = {
                messageContent: res.messageContent,
                emailRichContent: res.emailRichContent,
                emailContent: res.emailContent,
                smsContent: res.smsContent,
              }
              this[`${key}Form`].updateFields({[`${key}Content`]: { value: res[`${key}Content`] }})
              if (key === 'email') this[`${key}Form`].updateFields({"emailRichContent": { value: this.defaultcontent && this.defaultcontent.emailRichContent }})
              this.initContent();
            }
        } else {
            this[`${key}Form`].updateFields({[`${key}Content`]: { value: this.content[`${key}Content`] }})
            if (key === 'email') this[`${key}Form`].updateFields({"emailRichContent": { value: this.content.emailRichContent }})
        }
    }

    initContent = () => {
      this.content = this.defaultcontent
    }

    @action getUrgentNum = async (params) => {
        let res = Api.urgentNum(params)
        return res;
    }

    @action urgent = async (params) => {
        let res = Api.urgent(params)
        return res
    }
}